package com.hit.aircraftwar.activity;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.hit.aircraftwar.R;
import com.hit.aircraftwar.application.GameView;
import com.hit.aircraftwar.setter.ParameterSet;
import com.hit.aircraftwar.setter.TimeController;

import java.util.Objects;

public class MallActivity extends AppCompatActivity {

    private Button mallReturnButton;
    private Button buy_point;
    private Button buy_trackbullet;
    private Button buy_hp;
    private Button buy_fire;
    private int point;
    private TextView pointHave;

    @SuppressLint("SetTextI18n")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mall);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        Objects.requireNonNull(getSupportActionBar()).hide();

        mallReturnButton = findViewById(R.id.mallReturnButton);
        mallReturnButton.setOnClickListener(view -> {
            ParameterSet.INSTANCE.updatePoint(point);
            Intent intent = new Intent(MallActivity.this, MainActivity.class);
            startActivity(intent);
        });

        buy_point = findViewById(R.id.buy_point);
        buy_hp = findViewById(R.id.buy_hp);
        buy_trackbullet = findViewById(R.id.buy_trackbullet);
        buy_fire = findViewById(R.id.buy_fire);
        pointHave = findViewById(R.id.point);

        buy_point.setOnClickListener(view -> {
            if(point >= 250 && !ParameterSet.INSTANCE.pointDouble) {
                point -= 250;
                showSuccess();
                ParameterSet.INSTANCE.pointDouble = true;
            } else if(ParameterSet.INSTANCE.pointDouble){
                showRepeat();
            } else {
                showLackPoint();
            }
            pointHave.setText("当前积分：" + point);
        });

        buy_hp.setOnClickListener(view -> {
            if(point >= 100 && !ParameterSet.INSTANCE.hpDouble) {
                point -= 100;
                showSuccess();
                ParameterSet.INSTANCE.hpDouble = true;
            } else if(ParameterSet.INSTANCE.hpDouble){
                showRepeat();
            } else {
                showLackPoint();
            }
            pointHave.setText("当前积分：" + point);
        });

        buy_trackbullet.setOnClickListener(view -> {
            if(point >= 200 && !ParameterSet.INSTANCE.trackBulletActive) {
                point -= 200;
                showSuccess();
                ParameterSet.INSTANCE.trackBulletActive = true;
            } else if(ParameterSet.INSTANCE.trackBulletActive){
                showRepeat();
            } else {
                showLackPoint();
            }
            pointHave.setText("当前积分：" + point);
        });

        buy_fire.setOnClickListener(view -> {
            if(point >= 150 && !ParameterSet.INSTANCE.fireDouble) {
                point -= 150;
                showSuccess();
                ParameterSet.INSTANCE.fireDouble = true;
            } else if(ParameterSet.INSTANCE.fireDouble){
                showRepeat();
            } else {
                showLackPoint();
            }
            pointHave.setText("当前积分：" + point);
        });
    }

    private void showLackPoint(){
        final AlertDialog.Builder normalDialog = new AlertDialog.Builder(this);
        normalDialog.setTitle("");
        normalDialog.setMessage("积分不足！");
        normalDialog.setPositiveButton("确定",
                (dialog, which) -> {
                });
        // 显示
        normalDialog.show();
    }

    private void showSuccess(){
        final AlertDialog.Builder normalDialog = new AlertDialog.Builder(this);
        normalDialog.setTitle("");
        normalDialog.setMessage("购买成功！道具将在下次战斗生效！");
        normalDialog.setPositiveButton("确定",
                (dialog, which) -> {
                });
        // 显示
        normalDialog.show();
    }

    private void showRepeat(){
        final AlertDialog.Builder normalDialog = new AlertDialog.Builder(this);
        normalDialog.setTitle("");
        normalDialog.setMessage("此道具已生效，无需重复购买！");
        normalDialog.setPositiveButton("确定",
                (dialog, which) -> {
                });
        // 显示
        normalDialog.show();
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void onResume() {
        super.onResume();
        point = ParameterSet.INSTANCE.getPoint();
        System.out.println(point + "!!!!!!!!!!!!!!!!!!!!!!!!!!!");
        pointHave.setText("当前积分：" + point);
    }
}
